﻿using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Xpo;
using EasyXaf.EntityDesigner.BusinessObjects.Entities;
using EasyXaf.EntityDesigner.BusinessObjects.Entities.EntityProperties;
using System.ComponentModel;

namespace EasyXaf.EntityDesigner.BusinessObjects.Entities.ValidationRules;

/// <summary>
/// ValidationRule
/// </summary>
[XafDefaultProperty(nameof(Name))]
[DeferredDeletion(false)]
public abstract partial class ValidationRule : BaseObject
{
	private string _name;
	private EntityProperty _targetProperty;
	private string _errorMessage;
	private Entity _entity;

	/// <summary>
	/// 名称
	/// </summary>
	[XafDisplayName("名称")]
	[VisibleInListView(false)]
	public string Name
	{
		get => _name;
		set => SetPropertyValue(nameof(Name), ref _name, value);
	}

	/// <summary>
	/// 目标属性
	/// </summary>
	[XafDisplayName("目标属性")]
	[DataSourceProperty(nameof(TargetPropertyDataSource))]
	public EntityProperty TargetProperty
	{
		get => _targetProperty;
		set => SetPropertyValue(nameof(TargetProperty), ref _targetProperty, value);
	}

	/// <summary>
	/// 错误消息
	/// </summary>
	[XafDisplayName("错误消息")]
	[VisibleInListView(false)]
	public string ErrorMessage
	{
		get => _errorMessage;
		set => SetPropertyValue(nameof(ErrorMessage), ref _errorMessage, value);
	}

	/// <summary>
	/// 规则描述
	/// </summary>
	[XafDisplayName("规则描述")]
	[VisibleInDetailView(false)]
	public string Description
	{
		get => GetDescription();
	}

	/// <summary>
	/// 实体
	/// </summary>
	[XafDisplayName("实体")]
	[Association("ValidationRules-Entity")]
	public Entity Entity
	{
		get => _entity;
		set => SetPropertyValue(nameof(Entity), ref _entity, value);
	}

	[Browsable(false)]
	public IList<EntityProperty> TargetPropertyDataSource
	{
		get => GetTargetPropertyDataSource();
	}

	public ValidationRule(Session session)
		: base(session)
	{
	}

	private partial string GetDescription();

	private partial IList<EntityProperty> GetTargetPropertyDataSource();
}
